package com.qianyu.core.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.core.entity.bean.MoneyWater;
import com.qianyu.core.entity.dto.MoneyWaterDto;
import com.qianyu.core.helper.LoginHelper;
import com.qianyu.core.mapper.MoneyWaterMapper;
import com.qianyu.core.service.MoneyWaterService;
import com.qianyu.core.util.TimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MoneyWaterServiceImpl extends ServiceImpl<MoneyWaterMapper, MoneyWater> implements MoneyWaterService {
    @Override
    public Page<MoneyWater> pageList(MoneyWaterDto.MoneyWaterPageDto dto) {
        Page<MoneyWater> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<MoneyWater> wrapper = Wrappers.lambdaQuery();
        if (ObjUtil.isNotEmpty(dto.getStartTime())) {
            wrapper.ge(MoneyWater::getCreateTime, dto.getStartTime());
        }
        if (ObjUtil.isNotEmpty(dto.getEndTime())) {
            wrapper.le(MoneyWater::getCreateTime, TimeUtil.toDayLastTime(dto.getEndTime()));
        }
        if (dto.getIsIncome() != null) {
            wrapper.eq(MoneyWater::getIsIncome, dto.getIsIncome());
        }
        wrapper.eq(MoneyWater::getUserId, LoginHelper.getUserId());
        return (Page<MoneyWater>) page(page,wrapper);
    }
}
